<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>后台登录</title>
    <link rel="stylesheet" type="text/css" href="/layui/css/layui.css" />
    <link rel="stylesheet" type="text/css" href="/css/login.css"/>
</head>

<body>
<div class="m-login-bg">
    <div class="m-login">
        <h3>后台系统登录</h3>
        <div class="m-login-warp">
            <form class="layui-form">
                <div class="layui-form-item">
                    <input type="text" name="userName" id="userName" required lay-verify="required" placeholder="用户名" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <input type="text" name="password" id="password" required lay-verify="required" placeholder="密码" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-item m-login-btn">
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="login">登录</button>
                    </div>
                    <div class="layui-inline">
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
                <a href="/toRegister">立即注册</a>
            </form>
        </div>
    </div>
</div>
<script src="/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script>
    layui.use('form', function() {
        var form = layui.form,
            $ = layui.jquery,
            layer = layui.layer;

        //自定义验证规则
        form.verify({
            userName: function(value) {
                if(value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            },
            password: [/(.+){6,12}$/, '密码必须6到12位']
        });

        form.on('submit(login)', function(data) {
            $.ajax({
                url:'/login',
                data:data.field,
                type:'POST',
                success:function (data) {
                    if(data.code == 0){
                        layer.msg("登录成功",{timer:1000},function () {
                            window.location.href = "/main";
                        })
                    }else{
                        layer.msg(data.msg,{timer:1000},function () {
                        })
                    }
                },
                error:function (xhr, status, err) {
                    layer.msg("登录失败",{timer:1000});
                }
            });
            return false;
        });

    });
</script>
</body>

</html>